package com.zzyl.common.ai;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Slf4j
public class AIModelInvoker {

    @Autowired
    private AliAIProperties aliAIProperties;

    public String qianWenInvoker(String prompt) throws NoApiKeyException, InputRequiredException {
        Generation gen = new Generation();

        // 系统消息
        Message systemMsg = Message.builder()
                .role(Role.SYSTEM.getValue())
                .content("你是一个专业的医学界AI助手, 请以这个角色和定位回答用户的问题")
                .build();

        // 用户消息
        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content(prompt)
                .build();


        // 指定模型名称和APIKEY
        GenerationParam param = GenerationParam.builder()
                .apiKey(aliAIProperties.getApiKey())
                .model(aliAIProperties.getModel())
                .messages(Arrays.asList(systemMsg, userMsg))
                .resultFormat(GenerationParam.ResultFormat.TEXT)
                .build();

        return gen.call(param).getOutput().getText();
    }

}